//
//  UIColor+Extension.m
//  ChatForHot
//
//  Created by wr on 2018/10/9.
//  Copyright © 2018年 FLY. All rights reserved.
//

#import "UIColor+Extension.h"
#import "NSString+Extension.h"

static inline NSUInteger hexStrToInt(NSString *str) {
  uint32_t result = 0;
  sscanf([str UTF8String], "%X", &result);
  return result;
}

static BOOL hexStrToRGBA(NSString *str,
                         CGFloat *r, CGFloat *g, CGFloat *b, CGFloat *a) {
  str = [[str stringByTrim] uppercaseString];
  if ([str hasPrefix:@"#"]) {
    str = [str substringFromIndex:1];
  } else if ([str hasPrefix:@"0X"]) {
    str = [str substringFromIndex:2];
  }
  
  NSUInteger length = [str length];
  //         RGB            RGBA          RRGGBB        RRGGBBAA
  if (length != 3 && length != 4 && length != 6 && length != 8) {
    return NO;
  }
  
  //RGB,RGBA,RRGGBB,RRGGBBAA
  if (length < 5) {
    *r = hexStrToInt([str substringWithRange:NSMakeRange(0, 1)]) / 255.0f;
    *g = hexStrToInt([str substringWithRange:NSMakeRange(1, 1)]) / 255.0f;
    *b = hexStrToInt([str substringWithRange:NSMakeRange(2, 1)]) / 255.0f;
    if (length == 4)  *a = hexStrToInt([str substringWithRange:NSMakeRange(3, 1)]) / 255.0f;
    else *a = 1;
  } else {
    *r = hexStrToInt([str substringWithRange:NSMakeRange(0, 2)]) / 255.0f;
    *g = hexStrToInt([str substringWithRange:NSMakeRange(2, 2)]) / 255.0f;
    *b = hexStrToInt([str substringWithRange:NSMakeRange(4, 2)]) / 255.0f;
    if (length == 8) *a = hexStrToInt([str substringWithRange:NSMakeRange(6, 2)]) / 255.0f;
    else *a = 1;
  }
  return YES;
}

@implementation UIColor (Extension)

+ (instancetype)colorWithHexString:(NSString *)hexStr {
  CGFloat r, g, b, a;
  if (hexStrToRGBA(hexStr, &r, &g, &b, &a)) {
    return [UIColor colorWithRed:r green:g blue:b alpha:a];
  }
  return nil;
}

+ (nullable UIColor *)colorFromHexString:(NSString *)hexStr {
  return [self colorWithHexString:hexStr];
}

- (NSString *)hexString {
  return [self hexStringWithAlpha:NO];
}

- (NSString *)hexStringWithAlpha {
  return [self hexStringWithAlpha:YES];
}

- (NSString *)hexStringWithAlpha:(BOOL)withAlpha {
  CGColorRef color = self.CGColor;
  size_t count = CGColorGetNumberOfComponents(color);
  const CGFloat *components = CGColorGetComponents(color);
  static NSString *stringFormat = @"%02x%02x%02x";
  NSString *hex = nil;
  if (count == 2) {
    NSUInteger white = (NSUInteger)(components[0] * 255.0f);
    hex = [NSString stringWithFormat:stringFormat, white, white, white];
  } else if (count == 4) {
    hex = [NSString stringWithFormat:stringFormat,
           (NSUInteger)(components[0] * 255.0f),
           (NSUInteger)(components[1] * 255.0f),
           (NSUInteger)(components[2] * 255.0f)];
  }
  
  if (hex && withAlpha) {
    hex = [hex stringByAppendingFormat:@"%02lx",
           (unsigned long)(self.alpha * 255.0 + 0.5)];
  }
  return hex;
}

- (CGFloat)red {
  CGFloat r;
  if ([self getRed:&r green:0 blue:0 alpha:0]) {
    return r;
  }
  return 0;
}

- (CGFloat)green {
  CGFloat g;
  if ([self getRed:0 green:&g blue:0 alpha:0]) {
    return g;
  }
  return 0;
}

- (CGFloat)blue {
  CGFloat b;
  if ([self getRed:0 green:0 blue:&b alpha:0]) {
    return b;
  }
  return 0;
}

- (CGFloat)alpha {
  CGFloat a;
  if ([self getRed:0 green:0 blue:0 alpha:&a]) {
    return a;
  }
  return 0;
}

- (CGFloat)hue {
  CGFloat h;
  if ([self getHue:&h saturation:0 brightness:0 alpha:0]) {
    return h;
  }
  return 0;
}

- (CGFloat)saturation {
  CGFloat s;
  if ([self getHue:0 saturation:&s brightness:0 alpha:0]) {
    return s;
  }
  return 0;
}

- (CGFloat)brightness {
  CGFloat b;
  if ([self getHue:0 saturation:0 brightness:&b alpha:0]) {
    return b;
  }
  return 0;
}
@end
